package com.fanketly.funacg2.ui.viewmodels

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.fanketly.funacg2.data.TAG
import com.fanketly.funacg2.data.entity.HistoryEntity
import com.fanketly.funacg2.data.repo.HistoryRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class HistoryViewModel @Inject constructor(private val repo: HistoryRepo) : ViewModel() {
    init {
        Log.d(TAG, "HistoryViewModel_init ")
    }

    //    val uiState: StateFlow<List<HistoryEntity>> =
//        getAll().stateIn(
//            scope = viewModelScope,
//            started = SharingStarted.WhileSubscribed(5000), initialValue = emptyList()
//        )
    val pager: Flow<PagingData<HistoryEntity>> = Pager(
        PagingConfig(
            pageSize = 20, initialLoadSize = 20, enablePlaceholders = true, maxSize = 200
        )
    ) {
        getAll()
    }.flow.cachedIn(viewModelScope)

    private fun getAll() = repo.getAll()


    fun delete(id: Int) {
        viewModelScope.launch {
            repo.delete(id)
        }
    }
}